var app = getApp();
Page({
  data: {
    zheng: "",
    fan: "",
    zheng64: "",
    fan64: "",
    img_z: "", //正面照后缀名
    img_f: "", //反面照后缀名
    name_z: "front_img", //正面照字段
    name_f: "reverse_img", //反面照字段
  },
  onLoad: function(e) {
    var that = this;
    var openid = wx.getStorageSync("openid");
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/shibai',
      data: {
        openid: openid
      },
      method: "post",
      success(res) {
        console.log(res)
        that.setData({
          message: res.data.data
        })
      }
    })
  },
  //选择身份证正面照
  chooseImg: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      success(res) {
        var arr = res.tempFilePaths[0].split(".");
        that.setData({
          zheng: res.tempFilePaths[0],
          img_z: arr[arr.length - 1]
        })
        //图片转为base64
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0],
          encoding: "base64",
          success(res) {
            that.setData({
              zheng64: res.data
            })
          }
        })
      }
    })
  },
  //选择身份证反面照
  chooseImgfan: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      success(res) {
        var arr = res.tempFilePaths[0].split(".");
        that.setData({
          fan: res.tempFilePaths[0],
          img_f: arr[arr.length - 1]
        })
        //图片转为base64
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0],
          encoding: "base64",
          success(res) {
            that.setData({
              fan64: res.data
            })
          }
        })
      }
    })
  },
  //支付
  payMoney: function(e) {
    var openid = wx.getStorageSync("openid");
    var that = this;
    if (e.detail.value.name && e.detail.value.IDcardNum && that.data.img_z && that.data.img_f) {
      wx.request({
        url: 'https://www.mamingwang.cn/api/index/shiming',
        data: {
          openid: openid,
          uname: e.detail.value.name, //名字
          card: e.detail.value.IDcardNum, //身份证号
          img_z: that.data.img_z, //正面照名字
          img_f: that.data.img_f, //反面照名字
          name_z: that.data.name_z, //正面照字段
          name_f: that.data.name_f, //反面照字段
          image_z: that.data.zheng64, //正面照base64
          image_f: that.data.fan64 //反面照base64
        },
        method: "post",
        success(res) {
          console.log(res)
          if (res.data.code == 1) {
            wx.showToast({
              title: '上传成功',
              icon: "none",
              duration: 1500,
              mask:true,
              success() {
                setTimeout(function() {
                  wx.redirectTo({
                    url: '../renZhengIng/renZhengIng',
                  })
                }, 1500)
              }
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '个人信息与身份证照片不能为空',
        icon: "none",
        duration: 2000,
        mask:true
      })
    }
  }
})